/**
 * @description jwt 策略文件
 * @author LI
 * @date 2020-11-13 16:33
 */
import { PassportStrategy } from '@nestjs/passport'
import { Strategy, ExtractJwt, StrategyOptions } from 'passport-jwt'
import { secretKeys } from '../../config/secretKeys'

export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      // ExtractJwt.fromHeader('token')ExtractJwt.fromAuthHeaderAsBearerToken()
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: secretKeys.JWT_SECRET_KEY
    } as StrategyOptions)
  }

  async validate(payload: any) {
    return {
      id: payload.id,
      nickName: payload.nickName,
      loginEmail: payload.loginEmail,
      photoUrl: payload.photoUrl
    }
  }
}
